/**
 **@constructor {Segment}
 ** represent  data-seg='menu'
 **/
define([
    '../../bean/menu',
    'static',
    'vList2'
], function(menuBean, CONST) {
    extend(Menu, Segment); //继承 保持和 class-instance 分离语言的继承感觉。 放在最上面，因为
    function Menu() {
        var _this = this;
        var _super = arguments.callee.parent;
        /**
         ** 初始化页面配置
         **/
        this.init = function() {
            this._DOM = $("[data-seg='menu']"); //当前这个类 hold住的前端引用
            this._DOM.empty();
            this.children = []; //获得 piece 实例的引用
            menuBean.queryMenu(function(results) {
                var m = {
                    data: []
                };
                // var admanage = {
                //     icon: "",
                //     key: 235,
                //     url: "#!management",
                //     value: "management"
                // }
                // results[1].son.push(admanage)
                $.each(results, function(index, v) {
                    m.data.push(_this._getMenuItem(v));
                });
                _this.menu = _this._DOM.vList2(m);
                _this._DOM.on('shrink_complete expand_complete', function() {});
                _super.init.call(_this);
                _this.adjustArticle();
                _this._DOM.find('.list-item.list-leaf.active').removeClass('active');
                _this._DOM.find('.list-item a[href="#!"]').removeAttr('href');
                _this._DOM.find('.list-item[deep="1"]').last().css('margin-bottom', '60px')
                _this._DOM.find('a[href="' + window.location.hash + '"]').closest('.list-item.list-leaf').addClass('active');
                _this.event();
            });
        };


        this.event = function() {
            _this._DOM.find('.content-part').on('click', function(e) {
                var href = $(this).find('a[href]').attr('href');
                if (href) {
                    window.location.hash = href;
                } else {
                    if ($(this).find('.glyphicon.glyphicon-menu-up')[0]) {
                        $(this).find('.glyphicon.glyphicon-menu-up').trigger('click');
                    }
                }
            })
        }

        /**
         ** 消息
         **/
        this.update = function(e) {
                switch (e.type) {
                    case "SHRINK_EXPAND":
                        _this.menu.fold();
                        _this.adjustArticle();
                        break;
                }
            }
            /**
             * [adjustArticle 调整左侧部分]
             * @param  {[type]} e [description]
             * @return {[type]}   [description]
             */
        this.adjustArticle = function(e) {
            var $main = $('#layout-main');
            var $menu = $('.fmp-menu');
            $(window).trigger('menu-fold'); //触发一个菜单变化的事件

            $menu.getNiceScroll().remove();
            // $main.getNiceScroll().remove();
            if (_this._DOM.hasClass('mini-state')) {
                $main.addClass('shrink-expand')
                setTimeout(function() {
                    $menu.removeAttr('style');
                }, 300);
            } else {
                $main.removeClass('shrink-expand');
                setTimeout(function() {
                    $menu.niceScroll();
                }, 300);
            }

            setTimeout(function() {
                $(window).trigger('resize');
            },300);
        }

        this._getMenuItem = function(v) {
            var item = {};
            if (v.icon) {
                var _icon = v.icon;
                try {
                    _icon = JSON.parse(_icon);
                    if (_icon.type == 'icon'){
                        item.icon = '<i class="' + _icon.class + '"></i>';
                    } else{
                        //item.icon = '<i class="replace" minSrc="' + _icon.minSrc+ '" maxSrc="' + _icon.maxSrc+ '"><img src="' + _icon.minSrc +'"/></i>';
                        item.icon = {
                            big:_icon.maxSrc,sm:_icon.minSrc
                        }
                    }

                } catch (e){
                    item.icon = '<i class="' + _icon + '"></i>';
                }

            }
            if (v.value) {
                item.label = v.value;
            }
            if (v.url) {
                item.href = v.url;
            }
            if (v.son && v.son[0]) {
                var s = [];
                $.each(v.son, function(index, val) {
                    s.push(_this._getMenuItem(val));
                });
                item.sub = s;
            }
            return item;
        }
    }
    return new Menu();
});
